package com.dogdog.activity;

import java.util.HashMap;
import java.util.Map;

import com.android.volley.Request.Method;
import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.dogdog.bean.LoginDataBean;
import com.dogdog.bean.Status;
import com.dogdog.manager.C;
import com.dogdog.manager.MyApplication;
import com.dogdog.util.ToastTool;
import com.google.gson.Gson;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class Hand_login extends Activity implements OnClickListener {

	private TextView right_btn;
	private TextView title;
	private EditText et_accunt, et_pass;
	private Button btn_login;
	private RelativeLayout titlebar;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.login); 
		initView();
	}

	private void initView() {
		titlebar = (RelativeLayout) findViewById(R.id.rl_titlebar);
		title = (TextView) findViewById(R.id.tv_title);
		title.setText("登录");
		title.setOnClickListener(this);
		right_btn = (TextView) findViewById(R.id.tv_right);
		right_btn.setText("注册");
		right_btn.setOnClickListener(this);
		btn_login = (Button) findViewById(R.id.login);
		btn_login.setOnClickListener(this);
		et_accunt = (EditText) findViewById(R.id.login_account);
		et_pass = (EditText) findViewById(R.id.login_pass);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login:
			if (TextUtils.isEmpty(et_accunt.getText().toString())
				||TextUtils.isEmpty(et_pass.getText().toString())) {
				ToastTool.show(Hand_login.this,"账号和密码不能为空");
			}else{
			btn_login.setEnabled(true);
			StringRequest stringRequest = new StringRequest(Method.POST,C.URL.LOGIN, new Response.Listener<String>() {
				@Override
				public void onResponse(String response) {
					   Log.d("TAG1", response);  
					Gson gson = new Gson();
					Status status = gson.fromJson(response, Status.class);
					if (status.getState() == 1){
			         ToastTool.show(Hand_login.this,"登录成功");
			         LoginDataBean data= gson.fromJson(response,LoginDataBean.class);
			         data.getData().getFaceBitmap();
			         MyApplication.setMeData(data.getData());
			         
						Intent intent = new Intent(Hand_login.this, Hand_main1.class);
						startActivity(intent);
						finish();
					    }else{
					ToastTool.status(Hand_login.this,status);
					}
				}
			}, new ErrorListener() {
				@Override
				public void onErrorResponse(VolleyError error) {
					ToastTool.show(getApplicationContext(), error.getMessage());

				}
			}) {
				@Override
				protected Map<String, String> getParams() throws AuthFailureError {
					Map<String, String> appendHeader = new HashMap<String, String>();
					appendHeader.put("name", et_accunt.getText().toString());
					appendHeader.put("password", et_pass.getText().toString());
					return appendHeader;
				}
			};
			MyApplication.getHttpQueue().add(stringRequest);
			}
			break;
		case R.id.tv_right:
			Intent intent1 = new Intent(this, Hand_registered.class);
			startActivity(intent1);
			break;
		}
	}

}
